﻿using Microsoft.AspNetCore.Mvc;
using RobosimAPI.Class;

namespace RobosimAPI.Controllers
{
	[Route("back/Api2/Client/getEventList")]
	public class GetEventListController : ControllerBase
	{
		[HttpPost]
		public IActionResult GetEventList([FromForm] GetEventListRequest request)
		{
			if (!request.IsValid())
			{
				return Ok(new ErrorResponse { status = -1, errmsg = "_MISSING_REQUIRED_FIELDS_" });
			}
			Account account =new  Account(request.user_id);
			if (!account.IsValid())
			{
				return Ok(new ErrorResponse { status = -1, errmsg = "_INCORRECT_" });
			}
			int[] join= account.GetAllJoinedEvents();
			List<Event> @event = Event.GetEvents(request.index, request.index+ request.rows);
			EventListResponse response = new EventListResponse();
			response.status = 1;
			response.data = new Data();
			response.data.total_rows = Event.evevt_count;
			response.data.eventList = new List<EventListItem>();
			for(int i = 0; i < @event.Count; i++)
			{
				EventListItem item = new EventListItem();
				item.event_id = @event[i].event_id;
				item.event_title = @event[i].event_title;
				item.start_time = Tools.TimestampToTimeString(@event[i].start_time);
				item.event_test = @event[i].event_test;
				item.top = @event[i].top;
				item.event_url = @event[i].event_url;
				item.event_status = @event[i].event_status;
				item.applied = 0;
				item.groupList = new List<GroupListItem>();
				Group[] group = @event[i].GetGroups() ;
				for(int j = 0; j < group.Length; j++)
				{
					GroupListItem groupItem = new GroupListItem();
					groupItem.group_id = group[j].id;
					groupItem.group_name = group[j].title;
					groupItem.applied = 0;
					if(Array.IndexOf(join, group[j].id)>=0)
					{
						groupItem.applied = 1;
						item.applied = 1;
					}
					item.groupList.Add(groupItem);
				}
				response.data.eventList.Add(item);
			}
			return Ok(response);
		}
		public class GetEventListRequest
		{
			public int user_id { get; set; }
			public int jmd_id { get; set; }
			public string secret { get; set; }
			public string version { get; set; }
			public string region { get; set; }
			public int index { get; set; }
			public int rows { get; set; }
			public bool IsValid()
			{
				return user_id>0 && jmd_id>0 && secret!= null && version!= null && region!= null && index >= 0 && rows > 0;
			}
		}
		public class GroupListItem
		{
			public int group_id { get; set; }
			public string group_name { get; set; }
			public int applied { get; set; }
		}

		public class EventListItem
		{
			public int event_id { get; set; }
			public string event_title { get; set; }
			public string start_time { get; set; }
			public int event_test { get; set; }
			public int top { get; set; }
			public string event_url { get; set; }
			public int event_status { get; set; }
			public int applied { get; set; }
			public List<GroupListItem> groupList { get; set; }
		}

		public class Data
		{
			public int total_rows { get; set; }
			public List<EventListItem> eventList { get; set; }
		}

		public class EventListResponse
		{
			public int status { get; set; }
			public Data data { get; set; }
		}

	}
}
